package com.handpay.zztong.hp.ui;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.handpay.zztong.hp.R;
import com.handpay.zztong.hp.base.activity.ZZTongActivity;
import com.handpay.zztong.hp.adapter.AreaAdapter;
import com.handpay.zztong.hp.bean.BankArea;

import java.util.ArrayList;
import java.util.List;

public class HPBankAreaEditView extends EditText {
	
	AreaAdapter mBankAreaAdapter = null;
	private AlertDialog mAlertDialog;
	private OnBankAreaLisenter mAreaListener;
	private View mContainer;
	private EditText mEt;
	private ListView mBankAreaListView;

	private List<BankArea> areas=new ArrayList<BankArea>();
	public HPBankAreaEditView(Context context) {
		super(context);
		init();
	}
	public HPBankAreaEditView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}
	
	public HPBankAreaEditView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init();
	}
	private void areaDialog() {
		AlertDialog.Builder ab = new AlertDialog.Builder(this.getContext());
		TextView textView = new TextView(this.getContext());
		textView.setText(R.string.public_opening_bankProvince_hint);
		textView.setBackgroundResource(R.color.colorPrimary);
		textView.setTextColor(Color.WHITE);
		textView.setTextSize(16f);
		textView.setGravity(Gravity.CENTER_HORIZONTAL);
		LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
		textView.setPadding(0,20,0,20);
		textView.setLayoutParams(layoutParams);
		ab.setCustomTitle(textView);
		ab.setView(createContent());
		mAlertDialog = ab.create();
		mAlertDialog.show();
	}
	public void onStop() {
		closeDialog();
	}
	private void closeDialog() {
		try {
			if (mAlertDialog != null)
			{
				mAlertDialog.dismiss();
				mAlertDialog = null;
			}
		} catch (Exception e) {
			e.printStackTrace();
			mAlertDialog = null;
		}
	}
	public void init() {
		this.setFocusable(false);
		this.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				areaDialog();
			}});
		mBankAreaAdapter = new AreaAdapter(this.getContext());
	}
	/**
	 * 创建dialog的内容。这里每次都是重新创建对象。能否复用？
	 * 
	 * @return
	 */
	private View createContent() {
		mContainer = inflate(getContext(), R.layout.pop_subbank_dialog, null);
		mEt = (EditText)mContainer.findViewById(R.id.et_subbank);
		mEt.addTextChangedListener(mTextWatcher);
		mBankAreaListView = (ListView)mContainer.findViewById(R.id.sub_bank_list);
		mBankAreaListView.getLayoutParams().height = ZZTongActivity.screenHeight/2;
		mBankAreaListView.setDivider(this.getResources().getDrawable(R.color.bank_list_view_divider));
		mBankAreaListView.setDividerHeight(1);
		if (areas.size()>0){
			mBankAreaAdapter.refreshBankArea(areas);
		}
		mBankAreaListView.setAdapter(mBankAreaAdapter);
		mBankAreaListView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3)
            {
				closeDialog();
				if (null != mAreaListener) 
				{
					mAreaListener.onBankAreaSelected((BankArea)arg0.getAdapter().getItem(arg2));
				}
			}});
		return mContainer;
	}

	private TextWatcher mTextWatcher;
	@Override
	public void addTextChangedListener(TextWatcher watcher) {
		mTextWatcher = watcher;
	}
	public interface OnBankAreaLisenter {
		void onBankAreaSelected(BankArea province);
	}
	public void setLisenter(OnBankAreaLisenter listener) 
	{
		mAreaListener = listener;
	}
	public synchronized void refreshBankArea(List<BankArea> areas) 
	{
		this.areas=areas;
		mBankAreaAdapter.refreshBankArea(areas);
	}
	public BankArea  matchBankArea(String bankAreaName) 
	{
		if (TextUtils.isEmpty(bankAreaName)) 
		{
			return null;
		}
		BankArea a = null;
		for (int i = 0; i < mBankAreaAdapter.getCount(); i++) 
		{
			BankArea temp = (BankArea)mBankAreaAdapter.getItem(i);
			if (bankAreaName.equals(temp.getBankAreaName())) 
			{
				a = temp;
				break;
			}
		}
		return a;
	}

}
